//
// GPSMapEdit
// (c) Konstantin Galichsky (kg@geopainting.com), 2007
//
// Raster map mosaic downloaded from WWW.
//

# include "StdAfx.h"
# include "Resource.h"
# include "WebMap.h"
# include "MapView.h"
# include "Dialog.h"
# include "EditHelpers.h"
# include "PrintUnits.h"
# include "Settings.h"
# include "Wrappers.h"
# include "fast_floor.h"
# include "Globals.h"

# include "WebMap.inl"

bool g_bUseGMOnline;
bool g_bUseGMCache;
bool g_bWriteGMCache;
bool g_bWriteEarthSlicerNames;
bool g_bGMSetCookie;
DWORD g_dwGMVersion;

void GetSetting_GoogleMapsCache (char _strPath [MAX_PATH]) {
	GetSetting ("GoogleMapsCache", _strPath, MAX_PATH, "c:\\GoogleMaps"); // "C:\\EarthSlicer\\SatCache\\"
}

float GetWebMapShiftDXInM () {
	const int dx = GetSetting ("GMShiftDX", (DWORD) 0);
	return dx*0.1f;
}

float GetWebMapShiftDYInM () {
	const int dy = GetSetting ("GMShiftDY", (DWORD) 0);
	return dy*0.1f;
}

class CShiftWebMap : public CDialog {
  public:
	CShiftWebMap () : CDialog (IDD_EDIT_SHIFT) {}

	// Override.
	virtual bool OnInit (HWND _hwndFocus) {
		const float fDX = GetWebMapShiftDXInM (); 
		const float fDY = GetWebMapShiftDYInM ();

		if (g_DistanceUnit == unMetric) {
			SetDlgItemTextF (IDC_DX, "%.1f", fDX);
			SetDlgItemTextF (IDC_DY, "%.1f", fDY);
		} else {
			SetDlgItemTextF (IDC_DX, "%.0f", fDX*c_feet_per_m);
			SetDlgItemTextF (IDC_DY, "%.0f", fDY*c_feet_per_m);

			SetDlgItemText (IDC_UNIT_X, "ft");
			SetDlgItemText (IDC_UNIT_Y, "ft");
		}

		EnableDlgItem (IDOK,     false);
		EnableDlgItem (ID_APPLY, false);

		return true;
	}
	virtual void OnCommand (WORD _wCmd, WORD _wNotifyCode, HWND _hwndCtl) {
		switch (_wCmd) {
			case IDC_DX:
			case IDC_DY:
				if (_wNotifyCode == EN_CHANGE) {
					const bool bOK =
						ValidateAsFloat (GetDlgItem (IDC_DX)) &&
						ValidateAsFloat (GetDlgItem (IDC_DY));
					EnableDlgItem (IDOK,     bOK);
					EnableDlgItem (ID_APPLY, bOK);
				}
				break;

			case ID_APPLY:
			case IDOK: {
				float fDX = GetTextAsFloat (GetDlgItem (IDC_DX));
				float fDY = GetTextAsFloat (GetDlgItem (IDC_DY));
				if (g_DistanceUnit != unMetric) {
					fDX /= c_feet_per_m;
					fDY /= c_feet_per_m;
				}
				SaveSetting ("GMShiftDX", (DWORD) fast_floor (fDX*10 + .5f));
				SaveSetting ("GMShiftDY", (DWORD) fast_floor (fDY*10 + .5f));

				Refresh ();

				if (_wCmd == IDOK)
					EndDialog (0);
				break;
			}

			case IDCANCEL:
				EndDialog (0);
				break;
		}
	}
};

void ShiftWebMap (HWND _hWnd) {
	CShiftWebMap ().ShowModal (_hWnd);	
}

void ResetWebMapShift () {
	SaveSetting ("GMShiftDX", (DWORD) 0);
	SaveSetting ("GMShiftDY", (DWORD) 0);

	Refresh ();
}
